package 整数中1出现的次数;

import javax.smartcardio.ATR;

//1到n的整数，1 出现的个数
public class Solution
{
    //不用全局变量


    public int NumberOf1Between1AndN_Solution(int n)
    {
        int count=0;
        return NumberOf1Between1AndN_Solution(n,count);
    }

    //采用递归的方法
    public int NumberOf1Between1AndN_Solution(int n,int count)
    {
        //判断输入
        if (n==0)
            return 0;
        //转化成字符串，比较方便
        String str=n+"";
        if (str.length()>2)
        {
            //分开数字，245->1~45,45~245
            //处理后面的45~245
            String substring = str.substring(1);
            char[] chars = str.toCharArray();
            int c=chars[0]-'0';
            count+= c*(str.length()-1)*(Math.pow(10,str.length()-2));
            count=NumberOf1Between1AndN_Solution(Integer.valueOf(substring)+1,count);
        }
        else
        {
            //小于100的数的情况
            for (int i = 0; i <= n; i++)
            {
                 count=NumberOf1smallthan100(i,count);
            }
        }
        return count;
    }
    //对10位直接硬解
    public int NumberOf1smallthan100(int n,int count)
    {
        String str=n+"";
        char[] chars = str.toCharArray();
        if (chars[0] == '1')
            count++;
        if (chars.length==2)
        {
            if (chars[1] == '1')
                count++;
        }
        return count;
    }
//countDigitOne
    public static void main(String[] args)
    {
        Solution solution = new Solution();
        System.out.println(solution.NumberOf1Between1AndN_Solution(101));
    }
}
